package com.zhongyewx.teachercert;

import android.arch.persistence.room.Room;
import android.content.Context;
import android.support.test.runner.AndroidJUnit4;

import com.zhongyewx.teachercert.db.ZYTCDataBase;
import com.zhongyewx.teachercert.db.dao.ZYUserDao;
import com.zhongyewx.teachercert.db.entity.UserEntity;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

import static android.support.test.espresso.matcher.ViewMatchers.assertThat;
import static org.hamcrest.CoreMatchers.equalTo;

/**
 * 创建Room数据库的单元测试
 */
@RunWith(AndroidJUnit4.class)
public class SimpleEntityReadWriteTest {
    private ZYUserDao mUserDao;
    private ZYTCDataBase mDb;

    @Before
    public void createDb() {
        Context context = ZYApplication.getInstance().getApplication().getApplicationContext();
        mDb = Room.inMemoryDatabaseBuilder(context, ZYTCDataBase.class).build();
        mUserDao = mDb.getUserDao();
    }

    @After
    public void closeDb() throws IOException {
        mDb.close();
    }

    @Test
    public void writeUserAndReadInList() throws Exception {
        UserEntity user = new UserEntity();
        user.setUserGroupId(System.currentTimeMillis()+"");
        user.setUserName("ass");
        mUserDao.insert(user);
        mUserDao
                .loadUsersSync()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<UserEntity>>() {
                    @Override
                    public void accept(List<UserEntity> userEntities) throws Exception {

                    }
                });
//        mUserDao.insert(user);
        List<UserEntity> byName = mUserDao.queryAll();
        assertThat(byName.get(0), equalTo(user));
    }
}
